package com.ku.user.config;

import com.ku.user.common.RedisUtils;
import com.ku.user.common.utils.Result;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@Component
public class AuthResponseFilter extends ZuulFilter {
    @Resource
    private RedisUtils redisUtils;
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Result run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String token = request.getHeader("token");
        Object tokenStr = redisUtils.get(token);
        if(tokenStr == null){
            return Result.error("请重新登录");
        }
        return Result.success();
    }
}
